using System;
using System.Collections.Generic;
using System.IO;
using System.Media;
using System.Threading.Tasks;

namespace StarSkipGame
{
    /// <summary>
    /// 音效类型枚举
    /// </summary>
    public enum SoundType
    {
        BackgroundMusic,
        Explosion,
        PowerUp,
        GameOver,
        MenuSelect,
        AsteroidHit
    }

    /// <summary>
    /// 音效管理器类
    /// </summary>
    public class SoundManager : IDisposable
    {
        private Dictionary<SoundType, string> soundFiles;
        private SoundPlayer backgroundMusicPlayer;
        private SoundPlayer soundEffectPlayer;
        private bool isBackgroundMusicEnabled = true;
        private bool isSoundEffectsEnabled = true;
        private float backgroundMusicVolume = 0.7f;
        private float soundEffectsVolume = 1.0f;

        public bool IsBackgroundMusicEnabled
        {
            get { return isBackgroundMusicEnabled; }
            set { isBackgroundMusicEnabled = value; }
        }

        public bool IsSoundEffectsEnabled
        {
            get { return isSoundEffectsEnabled; }
            set { isSoundEffectsEnabled = value; }
        }

        public float BackgroundMusicVolume
        {
            get { return backgroundMusicVolume; }
            set { backgroundMusicVolume = Math.Max(0.0f, Math.Min(1.0f, value)); }
        }

        public float SoundEffectsVolume
        {
            get { return soundEffectsVolume; }
            set { soundEffectsVolume = Math.Max(0.0f, Math.Min(1.0f, value)); }
        }

        public SoundManager()
        {
            InitializeSoundFiles();
            backgroundMusicPlayer = new SoundPlayer();
            soundEffectPlayer = new SoundPlayer();
        }

        /// <summary>
        /// 初始化音效文件路径
        /// </summary>
        private void InitializeSoundFiles()
        {
            soundFiles = new Dictionary<SoundType, string>
            {
                { SoundType.BackgroundMusic, "sounds/background_music.wav" },
                { SoundType.Explosion, "sounds/explosion.wav" },
                { SoundType.PowerUp, "sounds/powerup.wav" },
                { SoundType.GameOver, "sounds/gameover.wav" },
                { SoundType.MenuSelect, "sounds/menu_select.wav" },
                { SoundType.AsteroidHit, "sounds/asteroid_hit.wav" }
            };
        }

        /// <summary>
        /// 播放背景音乐
        /// </summary>
        public void PlayBackgroundMusic()
        {
            if (!isBackgroundMusicEnabled) return;

            try
            {
                string musicFile = GetSoundFilePath(SoundType.BackgroundMusic);
                if (File.Exists(musicFile))
                {
                    backgroundMusicPlayer.SoundLocation = musicFile;
                    backgroundMusicPlayer.PlayLooping();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("播放背景音乐失败: " + ex.Message);
            }
        }

        /// <summary>
        /// 停止背景音乐
        /// </summary>
        public void StopBackgroundMusic()
        {
            try
            {
                backgroundMusicPlayer.Stop();
            }
            catch (Exception ex)
            {
                Console.WriteLine("停止背景音乐失败: " + ex.Message);
            }
        }

        /// <summary>
        /// 播放音效
        /// </summary>
        /// <param name="soundType">音效类型</param>
        public void PlaySoundEffect(SoundType soundType)
        {
            if (!isSoundEffectsEnabled) return;

            try
            {
                string soundFile = GetSoundFilePath(soundType);
                if (File.Exists(soundFile))
                {
                    // 使用异步播放避免阻塞
                    Task.Run(() =>
                    {
                        try
                        {
                            using (var player = new SoundPlayer(soundFile))
                            {
                                player.Play();
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("播放音效失败: " + ex.Message);
                        }
                    });
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("播放音效失败: " + ex.Message);
            }
        }

        /// <summary>
        /// 获取音效文件路径
        /// </summary>
        /// <param name="soundType">音效类型</param>
        /// <returns>文件路径</returns>
        private string GetSoundFilePath(SoundType soundType)
        {
            if (soundFiles.ContainsKey(soundType))
            {
                return soundFiles[soundType];
            }
            return string.Empty;
        }

        /// <summary>
        /// 创建默认音效文件（如果不存在）
        /// </summary>
        public void CreateDefaultSoundFiles()
        {
            string soundsDir = "sounds";
            if (!Directory.Exists(soundsDir))
            {
                Directory.CreateDirectory(soundsDir);
            }

            // 这里可以添加创建默认音效文件的逻辑
            // 由于无法直接生成音频文件，这里只是创建目录结构
            Console.WriteLine("音效目录已创建: " + soundsDir);
        }

        /// <summary>
        /// 设置音效开关
        /// </summary>
        public void SetSoundEffectsEnabled(bool enabled)
        {
            isSoundEffectsEnabled = enabled;
        }

        /// <summary>
        /// 设置背景音乐开关
        /// </summary>
        public void SetBackgroundMusicEnabled(bool enabled)
        {
            isBackgroundMusicEnabled = enabled;
            if (!enabled && backgroundMusicPlayer != null)
            {
                backgroundMusicPlayer.Stop();
            }
        }

        /// <summary>
        /// 设置音效音量
        /// </summary>
        public void SetSoundEffectsVolume(float volume)
        {
            soundEffectsVolume = Math.Max(0.0f, Math.Min(1.0f, volume));
        }

        /// <summary>
        /// 设置背景音乐音量
        /// </summary>
        public void SetBackgroundMusicVolume(float volume)
        {
            backgroundMusicVolume = Math.Max(0.0f, Math.Min(1.0f, volume));
        }

        /// <summary>
        /// 释放资源
        /// </summary>
        public void Dispose()
        {
            try
            {
                if (backgroundMusicPlayer != null)
                {
                    backgroundMusicPlayer.Stop();
                    backgroundMusicPlayer.Dispose();
                }
                if (soundEffectPlayer != null)
                {
                    soundEffectPlayer.Dispose();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("释放音效资源失败: " + ex.Message);
            }
        }
    }
} 